package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.coolsk.dao.MemberDao;
import cn.sumpu.app.coolsk.domain.Member;
import cn.sumpu.app.coolsk.domain.MemberDetail;
import cn.sumpu.app.coolsk.domain.MemberTicker;
import cn.sumpu.app.coolsk.domain.OutUser;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
public class MemberDaoImpl extends SqlSessionDaoSupport implements MemberDao{
	
	private static final Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);
	
	 /**
	  * 
	  */
	@Override
	public void insert(Member member)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MemberMapper.insert",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}
	
	 /**
	  * 
	  */
	@Override
	public void insertMemberInfo(MemberDetail memberDetail)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MemberMapper.insertMemberInfo",memberDetail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMemberInfo database failure.", e);
			}
			throw new AppBusinessException("insertMemberInfo database failure.", e);
		}
	}
	/**
	 * 取票人添加
	 */
	@Override
	public void insterMemberTicker(MemberTicker memberTicker)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MemberMapper.insertMemberTicker",memberTicker);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insterMemberTicker database failure.", e);
			}
			throw new AppBusinessException("insterMemberTicker database failure.", e);
		}
	}
	/**
	 * 修改取票人
	 */
	@Override
	public void updateMemberTicker(MemberTicker memberTicker)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MemberMapper.updateMemberTicker",memberTicker);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateMemberTicker database failure.", e);
			}
			throw new AppBusinessException("updateMemberTicker database failure.", e);
		}
	}
	
	 /**
	  * 
	  */
	@Override
	public void update(Member member)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.MemberMapper.update",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	/**
	 * 
	 */
	@Override
	public void updateMemberDetail(MemberDetail memberDetail)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.MemberMapper.updateMemberDetail",
					memberDetail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
	}
	
	/**
	 * 
	 */
	@Override
	public void deleteTicker(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.MemberMapper.deleteTicker",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteTicker database failure.", e);
			}
			throw new AppBusinessException("deleteTicker database failure.", e);
		}
	}
	/**
	 * 
	 */
	@Override
	public Member getMemberByMap(Map<String, Object> map) throws AppBusinessException{
		
		try {
			return (Member) getSqlSession()
					.selectOne(
							"cn.sumpu.app.coolsk.persistence.MemberMapper.getMemberByMap",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberByMap failure.", e);
			}
			throw new AppBusinessException("getMemberByMap failure.", e);
		}
	}
	/**
	 * 
	 */
	@Override
	public MemberDetail getMemberDetailByMap(Map<String, Object> map) throws AppBusinessException{
		
		try {
			return (MemberDetail) getSqlSession()
					.selectOne(
							"cn.sumpu.app.coolsk.persistence.MemberMapper.getMemberDetailByMap",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberDetailByMap failure.", e);
			}
			throw new AppBusinessException("getMemberDetailByMap failure.", e);
		}
	}
	
	@Override
	public void insert(OutUser outUser) throws AppBusinessException {
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.MemberMapper.insertOutUser",
					outUser);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
		
	}
	@Override
	public OutUser checkOutUser(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (OutUser) getSqlSession()
					.selectOne(
							"cn.sumpu.app.coolsk.persistence.MemberMapper.checkOutUser",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("checkOutUser failure.", e);
			}
			throw new AppBusinessException("checkOutUser failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberTicker> getTickerListById(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.MemberMapper.getTickerListById",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTickerListById database failure.", e);
			}
			throw new AppBusinessException("getTickerListById database failure.", e);
		}
	}
	//查询用户列表
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getMemberList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.MemberMapper.getMemberList",map);
			} catch (Exception e) {
				if (logger.isErrorEnabled()) {
					logger.error("getMemberList database failure.", e);
				}
				throw new AppBusinessException("getMemberList database failure.", e);
			}
	}
	//查询用户列表总条数
	@Override
	public Integer getMemberListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			   return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.MemberMapper.getMemberListCount",map);
			} catch (Exception e) {
				if (logger.isErrorEnabled()) {
					logger.error("getMemberListCount database failure.", e);
				}
				throw new AppBusinessException("getMemberListCount database failure.", e);
			}
	}
	
}
